﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

using EzDesk.DesktopClient.View;
using EzDesk.DesktopClient.ViewModel;
using EzDesk.DesktopClient.Event;
using System.Windows.Controls;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.Generic.WPF;
using EzDesk.Generic;

using Microsoft.Practices.Unity.Utility;
using System.Drawing;
using EzDesk.DesktopClient.ServiceLocator.ClientService;
namespace EzDesk.DesktopClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class IMClient : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            AddResources();
            RegistViews();
            InitViewModel();
            InitEvent();
            //ConfigManagement();
            StartShow();
        }

        private void AddResources()
        {

        }

        private void RegistManagment()
        {
            this.Container.RegisterInstance<AppManagement>(AppManagement.Instance);
        }

        private void RegistViews()
        {
            IMClientUnityContainerStatic.RegistViews();
        }

        protected void InitEvent()
        {
            FunctionNavigateEvent.Instance.Subscribe(this.OnFunctionNavigate);
            LoginEvent.Instance.Subscribe(this.OnLogin);
        }

        /// <summary>
        /// @Refactory divide into another class or split into multi methods
        /// </summary>
        /// <param name="args"></param>
        protected void OnFunctionNavigate(FunctionNavigateArgs args)
        {
            string url = args.Url;
            FunctionPageViewModel viewModel = ((IFunctionPageView)this.FunctionPageView).ViewModel as FunctionPageViewModel;
            viewModel.Navigate(url);
            this.FunctionPageView.ToBeside(this.MainView);
            this.FunctionPageView.Activate();
            this.FunctionPageView.Show();
        }

        protected void OnLogin(LoginArgs args)
        {
            args.IsSuccess.TrueDo(this.ConfigManagement);
        }

        protected void StartShow()
        {
            this.MainView.Show();
            this.NotificationView.Show();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            ShutDownEvent.Instance.Publish(null);
            base.OnExit(e);
        }
        #region InitView
        protected void InitViewModel()
        {
            InitMainView();
            InitNotificationView();
            InitFunctionPageView();
        }


        private void InitMainView()
        {
            ShellViewModel viewModel = new ShellViewModel();
            this.MainView = viewModel.View as Window;
        }
        private void InitNotificationView()
        {
            NotificationViewModel viewModel = new NotificationViewModel();
            this.NotificationView = viewModel.View as Window;
        }

        private void InitFunctionPageView()
        {
            FunctionPageViewModel viewModel = new FunctionPageViewModel();
            this.FunctionPageView = viewModel.View as Window;
        }
        #endregion InitView
        private void ConfigManagement()
        {
            this.DataManagement = DataManagement.Instance;
            this.UserManagement = UserManagement.Instance;
            this.UpdateDataManagement = UpdateDataManagement.Instance;
            this.ServiceConfigurationManagement = ServiceConfigurationManagement.Instance;
        }
    }
}
